import { TableColumnProps } from "ant-design-vue";
import { AcitivityDefItem, AcitivityDefStatus, UseActivityHookResult } from "/@/api/main/marketing/models";
import { CURDAction, CURDModalEvent, FormItemComponentType, FormItemData } from "/@/components/CURDDialog/models";
import { ref } from "vue";

export default function (): UseActivityHookResult {
  let addText = "新增活动档期",
    pageTitle = "活动档期定义",
    editText = "编辑活动档期",
    deleteText = "确认删除该活动档期吗？",
    innerColumns: TableColumnProps<Partial<AcitivityDefItem>>[] = [
      {
        title: "活动名称",
        dataIndex: "activityName",
      },
      {
        title: "活动起始日期",
        dataIndex: "startDate",
      },
      {
        title: "活动结束日期",
        dataIndex: "endDate",
      },
      {
        title: "活动主题",
        dataIndex: "activityTheme",
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: "操作",
        key: "oper",
        fixed: "right",
        width: 100,
      },
      // {
      //   title: "出库单类型",
      //   dataIndex: "deliveryType",
      //   customRender({ text }) {
      //     return getLabelByValueFromEnum(text, DeliveryType, "deliveryType");
      //   },
      // },
      // {
      //   title: "活动起始日期",
      //   dataIndex: "deliveryDate",
      //   customRender({ text }) {
      //     return dayjs(text).format("YYYY-MM-DD");
      //   },
      // },
    ];
  const model = ref<Partial<AcitivityDefItem>>({
    activityName: "",
    _range: [],
    activityTheme: "",
  });
  const modelFormSetting = ref<FormItemData<Partial<AcitivityDefItem>>[]>([
    {
      key: "activityName",
      required: true,
    },
    {
      key: "_range",
      type: FormItemComponentType.RANGEPICKER,
      label: "活动日期范围",
      required: true,
      props: {
        valueFormat: "YYYY-MM-DD",
      },
    },
    {
      key: "activityTheme",
      required: true,
    },
  ]);
  function innerBeforeOk({ model, action }: CURDModalEvent<Partial<AcitivityDefItem>>) {
    if (model) {
      if (model._range) {
        model.startDate = model._range[0];
        model.endDate = model._range[1];
        delete model._range;
      }
      if (action === CURDAction.CREATE) {
        model.status = AcitivityDefStatus.waiting;
      }
    }
  }
  function innerOpen({ record, action }: CURDModalEvent<Partial<AcitivityDefItem>>) {
    if (action === CURDAction.UPDATE && record) {
      if (record.startDate && record.endDate) {
        record._range = [record.startDate, record.endDate];
      }
    }
  }
  return { addText, pageTitle, editText, deleteText, innerColumns, model, modelFormSetting, innerBeforeOk, innerOpen };
}
